//
//  ViewController.m
//  shop
//
//  Created by Justsoso on 16/2/28.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "Shop.h"
#import "ShopView.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *shopsView;
@property (nonatomic,strong) NSArray *shops;

@end

@implementation ViewController

- (NSArray *)shops
{
    if (_shops == nil)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"goods" ofType:@"plist"];
        NSArray *tmpArr = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *mArr = [NSMutableArray array];
        for (NSDictionary *d in tmpArr)
        {
            Shop *s = [[Shop alloc] initWithDictionary:d];
            [mArr addObject:s];
        }
        _shops = mArr;
    }
    return _shops;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    int x = 5,y = 5;
    int width = 180,height = 190;
    int distance = self.shopsView.frame.size.width-2*(width+x);
    
    for (int i=0; i<self.shops.count; i++)
    {
        ShopView *shopView = [[ShopView alloc] init];
        shopView.frame = CGRectMake(x, y, width, height);
        shopView.backgroundColor = [UIColor whiteColor];
        [shopView setShop:self.shops[i]];
        [self.shopsView addSubview:shopView];
        x += width+distance;
        if ((i+1)%2 == 0)
        {
            x = 5;
            y += height+distance;
        }
    }
}



@end
